package com.mai4j.maiadmin.setup.db;

import com.mai4j.maiadmin.setup.Env;
import com.mai4j.maiadmin.setup.Helper;
import com.zaxxer.hikari.HikariDataSource;
import lombok.extern.slf4j.Slf4j;
import org.flywaydb.core.Flyway;
import picocli.CommandLine;

import java.util.concurrent.Callable;

/**
 * Created by 席有芳 on 2021/11/22.
 *
 * @author 席有芳
 */
@CommandLine.Command(name = "seed", description = "生成数据")
@Slf4j
public class Seed implements Callable<Integer> {

    @Override
    public Integer call() throws Exception {
        Env.Db db = Env.db();
        HikariDataSource ds = new HikariDataSource();
        ds.setJdbcUrl(String.format("%s/%s?%s", db.getUrl(), db.getDatabase(), db.getQuery()));
        ds.setUsername(db.getUsername());
        ds.setPassword(db.getPassword());
        Flyway flyway = Flyway.configure().locations("db/seed")
                .javaMigrations(Helper.getJavaMigrations("db.seed"))
                .table("flyway_seed")
                .baselineOnMigrate(true)
                .dataSource(ds).load();
        flyway.migrate();
        return 0;
    }
}
